import java.io.IOException;
import java.net.*;
import java.util.Scanner;

public class UdpEchoClient {
    private String serverIP;
    private int serverPort;
    private DatagramSocket socket;

    public UdpEchoClient(String serverIP,int serverPort) throws SocketException {
        this.serverIP=serverIP;
        this.serverPort=serverPort;
        socket=new DatagramSocket();
    }
    public void start() throws IOException {
        System.out.println("start");
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.print(">");
            if (!sc.hasNext()) {
                break;
            }
            String request=sc.next();

            DatagramPacket requestPacket=new DatagramPacket(request.getBytes(),
                    request.getBytes().length, InetAddress.getByName(serverIP),serverPort);
            socket.send(requestPacket);

            DatagramPacket responsePacket=new DatagramPacket(new byte[4096],4096);
            socket.receive(responsePacket);

            String response=new String(responsePacket.getData(),0,responsePacket.getLength());
            System.out.println(response);
        }

    }

    public static void main(String[] args) throws IOException {
        UdpEchoClient client=new UdpEchoClient("127.0.0.1",12520);
        client.start();

    }
}
